import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class AddCustomerFrame extends JDialog {	
	// Local Variables
	private JLabel _nameLbl, _ageLbl;
	private JTextField _nameField, _ageField;
	private JButton _addButton, _cancleButton;
	private JPanel _AddCustPanel, _AddCustPanelMain,
	_AddCustomerPanelButtons,_AddCustNameP, _AddCustAgeP;

	//	Peer objects
	private Person _newCustomer;

	public AddCustomerFrame(JFrame _parent)
	{
		super(_parent, true);
		this.setTitle("Add customer to the store");
		this.setResizable(false);
		this.setSize(300,110);
		_newCustomer = new Person("","");
		initComponents();
		setLayout();

		this.add(_AddCustPanel);
		this.setVisible(true);
	}

	private void initComponents()
	{
		_AddCustPanel = new JPanel(new BorderLayout());
		_AddCustPanelMain = new JPanel(new GridLayout(2,1));
		_AddCustomerPanelButtons = new JPanel(new GridLayout(1,2));
		_AddCustNameP = new JPanel(new FlowLayout());
		_AddCustAgeP = new JPanel(new FlowLayout());
		_nameLbl = new JLabel("Name:");
		_ageLbl = new JLabel("Age:");
		_nameField = new JTextField();
		_ageField = new JTextField();
		_addButton = new JButton("Add Customer");
		_cancleButton = new JButton("Cancle");

	}

	private void setLayout()
	{	
		_addButton.addActionListener(new AddCustomerListener());
		_cancleButton.addActionListener(new CancleListener());
		_ageField.setPreferredSize(new Dimension(120,20));
		_nameField.setPreferredSize(new Dimension(120,20));
		_AddCustPanel.add(_AddCustPanelMain,BorderLayout.CENTER);
		_AddCustPanel.add(_AddCustomerPanelButtons,BorderLayout.SOUTH);
		_AddCustPanelMain.add(_AddCustNameP);
		_AddCustNameP.add(_nameLbl);
		_AddCustNameP.add(_nameField);
		_AddCustPanelMain.add(_AddCustAgeP);
		_AddCustAgeP.add(_ageLbl);
		_AddCustAgeP.add(_ageField);
		_AddCustomerPanelButtons.add(_addButton);
		_AddCustomerPanelButtons.add(_cancleButton);
	}

	private boolean readInput()
	{
		if (_nameField.getText().trim().isEmpty())
		{
			JOptionPane.showMessageDialog(this, "You did not write in a name of the new customer.","Error", 1);
			return false;
		}
		else if(_ageField.getText().trim().isEmpty())
		{
			JOptionPane.showMessageDialog(this, "You did not write in an age of the new customer.","Error", 1);
			return false;
		}
		else if(!ageIsOK(_ageField.getText()))
		{
			return false;
		}
		else
		{
			return true;
		}
	}

	private boolean ageIsOK(String anAge)
	{
		int check;
		try
		{
			check = Integer.parseInt(anAge);
			if (check <= 0)
			{
				JOptionPane.showMessageDialog(this, "The age cant be negative or 0.","Error", 1);
				return false;
			}
			else
			{
				return true;
			}
		}
		catch (NumberFormatException e)
		{

			JOptionPane.showMessageDialog(this, "Please write with numbers only.","Error", 1);
			return false;
		}
	}

	private class AddCustomerListener implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{	
			if (readInput())
			{
				_newCustomer.set_name(_nameField.getText());
				_newCustomer.set_age(_ageField.getText());
				setVisible(false);
				setDefaultCloseOperation(0);
			}
		}
	}

	private class CancleListener implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{
			setVisible(false);
		}
	}

	/**
	 * @return the _newCustomer
	 */
	public Person get_newCustomer() {
		return _newCustomer;
	}
}
